package com.jrx.anytxn.transaction.timer.job.queryPaymentStatus.config;

import com.jrx.anytxn.ribbon.config.GrayVersionConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import com.jrx.anytxn.transaction.timer.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.timer.job.queryPaymentStatus.step.QueryPaymentStatusProcessor;
import com.jrx.anytxn.transaction.timer.job.queryPaymentStatus.step.QueryPaymentStatusWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 还款任务
 *
 * @author jing.xy
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class QueryPaymentStatusConfiguration {
    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;


    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private QueryPaymentStatusProcessor queryPaymentStatusProcessor;

    @Autowired
    private QueryPaymentStatusWriter queryPaymentStatusWriter;

    @Autowired
    DataSource dataSource;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    public GrayVersionConfig grayVersionConfig;

    @Bean
    public Job queryPaymentStatusJob() {
        return jobBuilderFactory.get("queryPaymentStatusJob")
                .start(queryPaymentStatusStep())
                .build();
    }

    @Bean
    public Step queryPaymentStatusStep() {
        return stepBuilderFactory.get("queryPaymentStatusStep")
                .<TlPaymentLog,TlPaymentLog>chunk(anytxnBatchProperties.getQueryPaymentStatusConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(queryPaymentStatusReader())
                .processor(queryPaymentStatusProcessor)
                .writer(queryPaymentStatusWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getQueryPaymentStatusConfig().getThrottleLimit())//多线程
                .build();
    }

    @Bean
    public SynchronizedItemStreamReader<TlPaymentLog> queryPaymentStatusReader() {

        MyBatisCursorItemReader<TlPaymentLog> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.transaction.mapper.ext.ExtTlPaymentLogMapper.selectBySumStatus");

        Map<String, Object> map = new HashMap<>();
        map.put("sumStatus", TransBizConstant.PAYMENT_LOG_SUM_STATUS_O);
        map.put("grayVersion", grayVersionConfig.getCurrentVersion());
        cursorItemReader.setParameterValues(map);
        //同步reader   线程安全
        SynchronizedItemStreamReader<TlPaymentLog> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

}
